package com.atguigu.lease.web.admin.custom.config;

import com.atguigu.lease.web.admin.custom.converter.StringTransformationEnumConverterFactor;
import com.atguigu.lease.web.admin.handleinterceptor.TokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置类，添加拦截器，转换器等配置类
 *
 * @Author mr.luo
 * @Date 21 5月 2025 11:43
 */
@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private TokenInterceptor tokenInterceptor;
//    @Autowired
//    private StringTransformationEnum stringToBaseEnumConverter;
    @Autowired
    private StringTransformationEnumConverterFactor stringToStringConverterFactor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/login/captcha", "/admin/login");
    }

    /**
     * 注册自定义转换器
     * 从String转换成枚举
     *
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
//        //优化前的转换器 Converter
//        registry.addConverter(stringToBaseEnumConverter);
        //优化后的转换器ConverterFactor
        registry.addConverterFactory(stringToStringConverterFactor);
    }
}
